package com.fastgwt.client.mvc.controller;

import java.util.List;

import com.fastgwt.client.Fastgwt;
import com.fastgwt.client.cmp.GwtContainer;
import com.fastgwt.client.mvc.controller.EmployeeController.EmployeeDriver;
import com.fastgwt.client.mvc.model.EmployeeQueryModel;
import com.fastgwt.client.mvc.view.EmployeeEditor;
import com.fastgwt.client.mvc.view.EmployeeQueryView;
import com.fastgwt.client.mvc.view.EmployeeView;
import com.fastgwt.shared.dto.Employee;
import com.fastgwt.shared.mvctemplate.controller.GwtController;
import com.fastgwt.shared.services.EmployeeService;
import com.fastgwt.shared.services.EmployeeServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.widget.core.client.event.RowDoubleClickEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.RowDoubleClickEvent.RowDoubleClickHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.info.Info;

public class EmployeeQueryController extends GwtController<Employee> {
	private EmployeeServiceAsync employeeService;

	interface EmployeeDriver extends
			SimpleBeanEditorDriver<Employee, EmployeeEditor> {
	}

	private EmployeeDriver driver;

	public EmployeeQueryController() {
		model = new EmployeeQueryModel();
		view = new EmployeeQueryView();

		addHandlersToView((EmployeeQueryView) view);

		employeeService = (EmployeeServiceAsync) GWT
				.create(EmployeeService.class);
		driver = GWT.create(EmployeeDriver.class);
		driver.initialize(((EmployeeQueryView) getView()).getEmployeeEditor());
		driver.edit(new Employee());

	}

	private void addHandlersToView(final EmployeeQueryView view) {
		view.getClearBtn().addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				// TODO Auto-generated method stub
				((EmployeeQueryView) EmployeeQueryController.this.getView())
						.getStore().clear();
			}
		});

		view.getListBtn().addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {

				try {
					// Info.display("firstName", firstName.getValue());
					employeeService.listAll(view.getEmployeeEditor()
							.getEmployeeName().getValue(), view
							.getEmployeeEditor().getEmployeeSurname()
							.getValue(), view.getEmployeeEditor().getJob()
							.getValue(), new AsyncCallback<List<Employee>>() {

						@Override
						public void onFailure(Throwable arg0) {
							// TODO Auto-generated method stub
							Info.display("failed", "rpc is failed");
						}

						@Override
						public void onSuccess(List<Employee> arg0) {
							((EmployeeQueryView) EmployeeQueryController.this
									.getView()).getStore().clear();
							((EmployeeQueryView) EmployeeQueryController.this
									.getView()).getStore().addAll(arg0);

						}
					});
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

		view.getGrid().addRowDoubleClickHandler(new RowDoubleClickHandler() {

			@Override
			public void onRowDoubleClick(RowDoubleClickEvent event) {
				// driver.initialize(Fastgwt.this);
				view.getGrid().getStore().get(event.getRowIndex());

				EmployeeController ec = new EmployeeController();
				ec.getDriver().edit(
						view.getGrid().getStore().get(event.getRowIndex()));
				GwtContainer.getInstance().setWidget(ec.getView());
				// driver.edit(new Employee());

				// createEmployeeForm.show();
				ec.getView().show();

			}
		});
	}

}